/****************************************************************************
* Copyright  2005 JinWei Bird Studio.	All rights reserved
*
* Filename: wf_xxx.cpp
* Description: cpp test file
*
* Version:1.0
* Create date: 05/03/2007
* Author: walter.fan@gmail.com
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wf_define.h"

#define BUFLEN 64

typedef int (* FnCmp)(void *, void *);

void * lsearch(void * key, void * base, int n, int elemSize, 
    FnCmp pFn)
{
    for(int i=0;i<n;i++) {
        void * elemAddr = (char*) base + i* elemSize;
        if(pFn(key, elemAddr) == 0) {
            return elemAddr;
        }
    }

    return NULL;
}

int IntCmp(void * elem1, void *elem2) {

    int * p1 = (int *)elem1;
    int * p2 = (int *)elem2;
    
    return *p1 - *p2;
    
}

void my_int_func(int x)
{
    printf( "the number is %d\n", x );
}

int test()
{
    void (*foo)(int);
    foo = &my_int_func;

    /* call my_int_func (note that you do not need to write (*foo)(2) ) */
    foo( 20111125 );
    /* but if you want to, you may */
    (*foo)( 1711 );

    return 0;
}
/*
void test() {

    int array[] = {4,2,3,7,11,6};
    //int size = 6;
    int number = 7;
    //FnCmp pFn = IntCmp;
    int * found = lsearch(&number, array, 6, sizeof(int), &IntCmp);
    if(found != NULL) {
        printf("got it\n");
    }

}
*/
int main(int argc, char *argv[])
{
	int nRet = 0;
	int ch = 0;
	FILE* fp = NULL;
	char szDataFile[BUFLEN] = "makefile";

	msg_trace("--- Walter test program ---");
	if(argc>1) {
		size_t nLen = strlen(argv[1]);
		if(nLen >= BUFLEN) {
			nLen = BUFLEN - 1;
		}
		strncpy(szDataFile, argv[1], nLen);
	} else {
		test();
	}

	if((fp = fopen(szDataFile,"r"))==NULL)
	{
		err_trace("error to open file" << szDataFile);
		nRet = -1;
		goto WF_EXIT;
	}

	ch = getc( fp );
	PRINT_HEX(ch);
	while( ch != EOF && ch != '\n') {
	     putc( ch, stdout );
	     ch = getc( fp );
	}

	fclose(fp);
WF_EXIT:
	putc('\n', stdout);
	msg_trace("--- end ---");
	return nRet;


}
